package cn.xspace.wnx.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.imcommon.utils.AppUtil;
import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.wnx.R;
import cn.xspace.wnx.view.QuickAlphabeticBar;

/**
 * Created by liuchen on 2016/10/24.
 */

public class ContactListAdapter extends BaseAdapter {
    private static final String TAG = "ContactListAdapter";
    private Context mContext;
    private LayoutInflater inflater;
    private QuickAlphabeticBar alphabeticBar;
    private List<OrgDeptUserBean> list;
    private List<String> letterList = new ArrayList<>();
    private boolean isShowCheckBox = false;
    private boolean isShowAlphea = true;
    private String groupOwnerId = "";
    private String userId = "";

    public boolean isShowAlphea() {
        return isShowAlphea;
    }

    public void setShowAlphea(boolean showAlphea) {
        isShowAlphea = showAlphea;
    }

    public boolean isShowCheckBox() {
        return isShowCheckBox;
    }

    public void setShowCheckBox(boolean showCheckBox) {
        isShowCheckBox = showCheckBox;
    }

    public ContactListAdapter(Context context, List<OrgDeptUserBean> list,
                              QuickAlphabeticBar alpha, boolean isShowCheckBox) {
        this.list = list;
        mContext = context;
        userId = MyInfoUtil.getMyUserid(mContext);
        inflater = ((Activity) mContext).getLayoutInflater();
        alphabeticBar = alpha;
        this.isShowCheckBox = isShowCheckBox;
//        ObservableUtil.getAlphaIndexer(list).subscribeOn(Schedulers.io())g
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<HashMap<String, Integer>>() {
//                    @Override
//                    public void call(HashMap<String, Integer> stringIntegerHashMap) {
//                        alphabeticBar.setAlphaIndexer(stringIntegerHashMap);
//                    }
//                });
        if (alpha == null) {
            return;
        }
        initAlpha(list);
    }

    public void setGroupOwnerId(String groupOwnerId) {
        this.groupOwnerId = groupOwnerId;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public void initAlpha(List<OrgDeptUserBean> list) {
        LogUtil.d(list.toString());
        TreeMap<String, Integer> alphaIndexer = new TreeMap<String, Integer>();
        for (int i = 0; i < list.size(); i++) {
            OrgDeptUserBean info = list.get(i);
            if (!TextUtils.isEmpty(info.shotKey)) {
                try {
                    Integer.parseInt(info.shotKey);
                    if (!alphaIndexer.containsKey("#")) {
                        alphaIndexer.put("#", i);
                    }
                } catch (Exception e) {
                    if (!alphaIndexer.containsKey(info.shotKey.toUpperCase())) {
                        alphaIndexer.put(info.shotKey.toUpperCase(), i);
                    }
                }
            } else {
                if (!alphaIndexer.containsKey("#")) {
                    alphaIndexer.put("#", i);
                }
            }
        }
        LogUtil.d(TAG, "alphaIndexer >>" + alphaIndexer.toString());
        Set<String> sectionLetters = alphaIndexer.keySet();
        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
        Collections.sort(sectionList);
        String[] sections = new String[sectionList.size()];
        sectionList.toArray(sections);
        alphabeticBar.setAlphaIndexer(alphaIndexer);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LogUtil.d(TAG, "getView >>" + position);
        OrgDeptUserBean mFriend = list.get(position);
        ViewHolder mViewHodler;
//        if (convertView == null) {
        mViewHodler = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.address_books_item, null);
        mViewHodler.findView(convertView);
        convertView.setTag(mViewHodler);
//        } else {
//            mViewHodler = (ViewHolder) convertView.getTag();
//        }
        setData(mViewHodler, mFriend, position);
        setListener(mViewHodler, mFriend, position);
        return convertView;
    }


    protected void setListener(final ViewHolder mViewHodler, final OrgDeptUserBean bean, final int position) {
        mViewHodler.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iOrgUserCheckListener != null) {
                    iOrgUserCheckListener.orgUserCheck(position, bean, mViewHodler.checkBox.isChecked());
                }
            }

        });
    }

    private void setData(ViewHolder viewHolder, OrgDeptUserBean entity, int position) {
        //头像显示
//            ImageUtils.showImage(user_logo, viewHolder.logoIv, ImageLoader.LoadType.URL);

        if (isShowCheckBox) {
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            viewHolder.checkBox.setChecked(entity.isSelectd);
            viewHolder.checkBox.setEnabled(entity.isCheckBoxEnable);

//            if (!TextUtils.isEmpty(groupOwnerId) && groupOwnerId.equals(entity.user_id)) {
//                viewHolder.checkBox.setVisibility(View.INVISIBLE);
//            }

//            if (userId.equals(entity.user_id)) {
//                viewHolder.checkBox.setVisibility(View.INVISIBLE);
//            }
        } else {
            viewHolder.checkBox.setVisibility(View.GONE);
        }
        HeadShowUtil.headShow(mContext, entity.user_logo, entity.user_sex, viewHolder.headerIV, HeadShowUtil.HeadType
                .UserLogo);
        viewHolder.contactInfoNameTV.setText(entity.user_name);
        if (!TextUtils.isEmpty(entity.is_stop)) {
            if ("0".equals(entity.is_stop)) {
                viewHolder.stopStatus.setVisibility(View.VISIBLE);
            } else {
                viewHolder.stopStatus.setVisibility(View.GONE);
            }
        }

        if (isShowAlphea) {
            String currentStr = AppUtil.getAlpha(entity.shotKey);
//        LogUtil.d("currentStr" + currentStr);
            String previewStr = (position - 1) >= 0 ? AppUtil.getAlpha(list
                    .get(position - 1).shotKey) : " ";
//        LogUtil.d("previewStr" + previewStr);
            if (!previewStr.equals(currentStr)) {
                viewHolder.contactItemAlphaTV.setVisibility(View.VISIBLE);
                viewHolder.contactItemAlphaTV.setText(currentStr);
            }
        } else {
            viewHolder.contactItemAlphaTV.setVisibility(View.GONE);
        }

    }

    static class ViewHolder {
        ImageView headerIV;
        TextView contactItemAlphaTV;
        TextView contactInfoNameTV;
        TextView stopStatus;
        ImageView mLineImg;
        CheckBox checkBox;

        public void findView(View v) {
            this.headerIV = (RoundAngleImageView) v.findViewById(R.id.imagview_header);
            this.contactItemAlphaTV = (TextView) v.findViewById(R.id.contact_list_item_alpha);
            this.contactInfoNameTV = (TextView) v.findViewById(R.id.textview_name);
            this.stopStatus = (TextView) v.findViewById(R.id.tv_stop_status);
            this.mLineImg = (ImageView) v.findViewById(R.id.line_ing);
            this.checkBox = (CheckBox) v.findViewById(R.id.check_select);
        }


    }

    private static IOrgUserCheckListener iOrgUserCheckListener;

    public void setiOrgUserCheckListener(IOrgUserCheckListener iOrgUserCheckListener) {
        this.iOrgUserCheckListener = iOrgUserCheckListener;
    }

    public interface IOrgUserCheckListener {
        void orgUserCheck(int position, OrgDeptUserBean bean, boolean isChecked);
    }

    public void updateSingleView(int positon) {


    }


}
